package com.zhenyi.xhc.core;

import com.gargoylesoftware.htmlunit.javascript.host.file.File;
import com.qiniu.common.QiniuException;
import com.utils.PathUtils;
import com.zhenyi.api.file.QNUtils;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.text.StrFormatter;
import cn.hutool.http.HttpUtil;

public class QRCodeUtils {

	public static void main(String[] args) {
		String url = qrcodeImage("abcde", "12345", "title", "time");
		System.out.println();
		System.out.println(url);
		// getQRCode("ZhenyiTech");
	}

	public static String getQRCode(String code) {
		try {
			// http://lets.zhen-yee.com//tkQrcode/ZhenyiTech.png
			String qrcodeImageUrl = "http://release.zhen-yee.com/api/qrcode?lt=1&size=100&text=" + code;
			System.out.println(qrcodeImageUrl);
			String path = PathUtils.getFile("data", "xhc", "qrcodeImage" + code + ".png");
			System.out.println(path);
			java.io.File file = new java.io.File(path);
			HttpUtil.downloadFile(qrcodeImageUrl, path);
			QNUtils qnUtils = new QNUtils("lets", "z2AFlN5J711q7CcKbabHakkrsj6IyBsBSqpSw_Kg",
					"QsjUX9QDnUrGxpgBWmnY8T6-ciGCdhhYIXJaauT0");
			String qnurl = qnUtils.upload(file, "tkQrcode/" + code + ".png");
			// System.out.println(qnurl);
			return "http://lets.zhen-yee.com/" + qnurl;
		} catch (Exception e) {
			return "";
		}
	}

	public static String qrcodeImage(String code, String no, String ticketTitle, String time) {
		String url = "https://release.zhen-yee.com/xhc/ticket/bg.png?watermark/3/image/{}/"
				+ "dissolve/100/gravity/NorthEast/dx/75/dy/150/ws/0.3/wst/0"
				+ "/text/{}/font/6buR5L2T/fontsize/1200/fill/d2hpdGU=/dissolve/100/gravity/"
				+ "NorthWest/dx/100/dy/180/text/{}/font/6buR5L2T/fontsize/1200/fill/d2hpdGU=/dissolve/100/gravity/NorthWest/dx/220/dy/250/"
				+ "text/{}/font/6buR5L2T/fontsize/550/fill/d2hpdGU=/dissolve/100/gravity/SouthEast/dx/50/dy/170"
				+ "/text/{}/font/6buR5L2T/fontsize/1150/fill/d2hpdGU=/dissolve/100/gravity/South/dx/0/dy/60" + ""
				+ "|imageMogr2/rotate/90";
		System.out.println(url);
		String qrcodeImageUrl = "http://www.liantu.com/api.php?w=100&text=" + code;
		qrcodeImageUrl = "https://i.loli.net/2018/05/11/5af50d7591443.jpg";
		qrcodeImageUrl = "http://res.etcr.com.cn/testing/api.php.png";
		qrcodeImageUrl = getQRCode(code);
		// String qrcodeImageUrl =
		// "http://release.zhen-yee.com/api/qrcode?size=200&lt=1&text=" + code;
		System.out.println("qrcodeImageUrl : " + qrcodeImageUrl);
		// qrcodeImageUrl =
		// "http://zhenyi.asuscomm.com:9090/api/qrcode?size=200&lt=1&text=" +
		// code;
		// http://release.zhen-yee.com/api/qrcode?text=Qzy8xnym
		qrcodeImageUrl = Base64.encode(qrcodeImageUrl);
		String title = "出出帮火车奇妙世界";
		title = Base64.encode(title);
		title = title.replace("+", "-");
		title = title.replace("/", "_");
		// String ticketTitle = "抢先体验票";
		ticketTitle = Base64.encode(ticketTitle);
		ticketTitle = ticketTitle.replace("+", "-");
		ticketTitle = ticketTitle.replace("/", "_");

		String ticketNo = "票号码:" + no;
		ticketNo = Base64.encode(ticketNo);
		ticketNo = ticketNo.replace("+", "-");
		ticketNo = ticketNo.replace("/", "_");

		String ticketTime = time;
		ticketTime = Base64.encode(ticketTime);
		ticketTime = ticketTime.replace("+", "-");
		ticketTime = ticketTime.replace("/", "_");

		url = StrFormatter.format(url, qrcodeImageUrl, title, ticketTitle, ticketNo, ticketTime);
		return url;
	}
}
